﻿using System.Text.Json;

using ExhibitionHall.Web.Entry;

using Furion;

using Mapster;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace OOS.Web.Core
{
    public class Startup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddConsoleFormatter();
            
            //JWT鉴权
            services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);

            //跨域
            services.AddCorsAccessor();

            //控制器
            services.AddControllers()
                    .AddInjectWithUnifyResult()
                    .AddJsonOptions(options =>
                    {
                        options.JsonSerializerOptions.Converters.AddDateTimeTypeConverters("yyyy-MM-dd HH:mm:ss");
                    });

            //SqlSugar
            services.AddSqlsugarSetup(App.Configuration);

            //对象数据全局映射
            TypeAdapterConfig.GlobalSettings.Default.PreserveReference(true);
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCorsAccessor();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseInject(string.Empty);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
